<template>
  <div class="account-dropdown">
    <div class="account-trigger" @mouseenter="showDropdown = true" @mouseleave="showDropdown = false">
      <div class="trigger-line-1">{{ $t('您好') }}，{{ userStore.isLogin ? userStore.userInfo?.member?.nickName : $t('请登录') }}</div>
      <div class="trigger-line-2">{{ $t('账户和心愿单') }} <i class="fas fa-caret-down"></i></div>
    </div>

    <div :class="['dropdown-panel', { 'show': showDropdown }]"
         @mouseenter="showDropdown = true"
         @mouseleave="showDropdown = false">
      <div class="panel-arrow"></div>

      <!-- 登录区域 - 未登录时显示 -->
      <div class="auth-section" v-if="!userStore.isLogin">
        <button class="signin-button" @click.prevent="handleLogin">
          {{ $t('立即登录') }}
        </button>
        <div class="register-hint">
          <span>{{ $t('新用户？') }}</span>
          <a href="#" @click.prevent="handleRegister">{{ $t('免费注册') }}</a>
        </div>
      </div>

      <div class="section-divider"></div>

      <!-- 主菜单区域 -->
      <div class="menu-section">
        <!-- 内容区域 -->
        <div class="menu-content">
          <!-- 左侧资产 -->
          <div class="content-left">
            <div class="menu-title">{{ $t('财务资产') }}</div>
            <ul>
              <li>
                <a href="#" class="menu-link" @click.prevent="handleMenuClick('/assets/recharge')">
                  <span>{{ $t('账户储值') }}</span>
                  <i class="fas fa-chevron-right"></i>
                </a>
              </li>
              <li>
                <a href="#" class="menu-link" @click.prevent="handleMenuClick('/assets/withdraw-select')">
                  <span>{{ $t('账户提款') }}</span>
                  <i class="fas fa-chevron-right"></i>
                </a>
              </li>
              <li>
                <a href="#" class="menu-link" @click.prevent="goToAssets">
                  <span>{{ $t('钱包余额') }}</span>
                  <i class="fas fa-chevron-right"></i>
                </a>
              </li>
            </ul>
          </div>

          <!-- 中间分割线 -->
          <div class="content-divider"></div>

          <!-- 右侧账户 -->
          <div class="content-right">
            <div class="menu-title">{{ $t('我的账户') }}</div>
            <ul>
              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account')"><span>{{ $t('我的账户') }}</span><i class="fas fa-chevron-right"></i></a></li>
              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account/orders')"><span>{{ $t('您的订单') }}</span><i class="fas fa-chevron-right"></i></a></li>
              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account/profile')"><span>{{ $t('我的资料') }}</span><i class="fas fa-chevron-right"></i></a></li>
              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account/security')"><span>{{ $t('登录和安全') }}</span><i class="fas fa-chevron-right"></i></a></li>
              <!-- <li><a href="#" class="menu-link" @click.prevent="promoteFriends"><span>{{ $t('推广好友') }}</span><i class="fas fa-chevron-right"></i></a></li> -->
<!--              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account/messages')"><span>{{ $t('我的消息') }}</span><i class="fas fa-chevron-right"></i></a></li>-->
              <li><a href="#" class="menu-link" @click.prevent="handleMenuClick('/account/customer-service')"><span>{{ $t('客户服务') }}</span><i class="fas fa-chevron-right"></i></a></li>
              <li v-if="userStore.isLogin">
                <a href="#" class="menu-link" @click.prevent="handleLogout">
                  <span>{{ $t('退出') }}</span>
                  <i class="fas fa-chevron-right"></i>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from "@/store/modules/user.js";
import {ElMessage, ElMessageBox} from "element-plus";
import {useI18n} from "vue-i18n";
const { t } = useI18n();

const userStore = useUserStore()
const router = useRouter()
const showDropdown = ref(false)

const handleLogin = () => {
  router.push('/login')
  showDropdown.value = false
}

const handleRegister = () => {
  router.push('/register')
  showDropdown.value = false
}

const handleLogout = () => {
  userStore.logOut().then(() => {
    window.location.reload()
    showDropdown.value = false
  })

}

const promoteFriends = () => {
  if (!userStore.isLogin) {
    ElMessage.warning(t('请先登录'))
    return
  }
  const url = `https://tw-coinget.com/#/register?inviteCode=${userStore.userInfo.member.inviteCode}`
  ElMessageBox.alert(url, t('推广好友链接'), {
    confirmButtonText: t('复制'),
    callback: async () => {
      await navigator.clipboard.writeText(url);
      ElMessage.success(t('复制成功'))
    },
  })
}

const handleMenuClick = (path) => {
  if (!userStore.isLogin) {
    router.push('/login')
  } else {
    router.push(path)
  }
  showDropdown.value = false
}

const goToAssets = () => {
  if (!userStore.isLogin) {
    router.push('/login')
  } else {
    router.push('/assets')
  }
  showDropdown.value = false
}
</script>

<style scoped>
.account-dropdown {
  position: relative;
  height: 100%;
  display: flex;
  align-items: center;
}

.account-trigger {
  cursor: pointer;
  padding: 2px 8px;
  border: 1px solid transparent;
  border-radius: 2px;
}

.account-trigger:hover {
  border-color: #fff;
}

.trigger-line-1 {
  color: #fff;
  font-size: 11px;
  line-height: 14px;
  margin-bottom: 1px;
}

.trigger-line-2 {
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  line-height: 14px;
  display: flex;
  align-items: center;
}

.trigger-line-2 i {
  font-size: 12px;
  margin-left: 4px;
}

.dropdown-panel {
  position: absolute;
  right: 0;
  top: 100%;
  width: 460px;
  background: white;
  border-radius: 6px;
  box-shadow: 0 2px 15px rgba(0, 0, 0, 0.15);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-2px);
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1000;
}

.dropdown-panel.show {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.panel-arrow {
  position: absolute;
  top: -8px;
  right: 20px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 8px solid white;
  filter: drop-shadow(0 -2px 2px rgba(0, 0, 0, 0.08));
}

.auth-section {
  padding: 15px 15px;
  text-align: center;
}

.signin-button {
  width: 150px;
  padding: 6px 12px;
  margin-bottom: 6px;
  background-image: linear-gradient(#f7dfa5, #f0c14b);
  border: 1px solid #a88734;
  border-radius: 3px;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
}

.signin-button:hover {
  background-image: linear-gradient(#f5d78e, #eeb933);
}

.register-hint {
  font-size: 12px;
  color: #444;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
}

.register-hint a {
  color: #0066c0;
  text-decoration: none;
}

.register-hint a:hover {
  color: #c45500;
  text-decoration: underline;
}

.section-divider {
  border-top: 1px solid #eee;
  margin: 0;
}

.menu-section {
  padding: 12px;
}

.menu-header {
  display: flex;
  margin-bottom: 12px;
  font-size: 13px;
  font-weight: bold;
  color: #333;
}

.header-left {
  flex: 1;
  padding-left: 20px;
}

.header-right {
  flex: 1;
  padding-left: 45px;
}

.menu-content {
  display: flex;
  min-height: 180px;
}

.content-left, .content-right {
  flex: 1;
}

.menu-title {
  font-weight: bold;
  font-size: 13px;
  color: #333;
  margin-bottom: 8px;
  padding: 0 20px;
}

.content-divider {
  width: 1px;
  background-color: #eee;
  margin: 0 12px;
}

ul {
  list-style: none;
  padding: 0 20px;
  margin: 0;
}

li {
  margin-bottom: 1px;
}

.menu-link {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 0;
  color: #444;
  text-decoration: none;
  font-size: 13px;
  transition: all 0.2s ease;
}

.menu-link:hover {
  color: #c45500;
  padding-left: 2px;
}

.menu-link:hover span {
  text-decoration: underline;
}

.menu-link i {
  font-size: 10px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.menu-link:hover i {
  opacity: 1;
}
</style>
